package com.wac.data.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
public interface ArticleMapper extends BaseMapper<Article> {

	// 分页查询所有文章 时间倒序
	@Select("select article.*,watched.number as watchedNum,t.commentNum from article,watched,(select article.id,count(`comment`.id) as commentNum "
			+ "from article left join `comment` on `comment`.comment_id = article.id "
			+ "group by article.id) as t where t.id = article.id and watched.id = article.id order by time desc")
	Page<Article> selectAll(Page page);

	// 分页查询某个用户的所有文章
	@Select("select * from article where user_id = #{userId} order by time desc")
	Page<Article> selectByUserId(Page<Article> page);

	//获取某用户的文章数量
	@Select("select count(*) from article where user_id = #{userId}")
	int getArticleNum(String userId);

	// 根据id查询某一条文章
	// @Select("select * from article where id = #{id}")
	// Article selectById(String id);

	@Select("select * from article where user_id = #{userId} order by time desc")
	List<Article> selectByUserIdDesc(String id);

	@Select("select * from article where user_id = #{userId} order by time asc")
	List<Article> selectByUserIdAsc(String id);


	// 插入文章
	@Insert("insert into article (title,content,cover,zone,user_id) values(#{title},#{content},#{cover},#{zone},#{userId})")
	int addArticle(String title, String content, String cover, String zone, String userId);


}
